package com.example.malltest0.service.impl;


import com.example.malltest0.common.api.CommonResult;
import com.example.malltest0.common.utils.RedisUtils;
import com.example.malltest0.mbg.mapper.PlatformMapper;
import com.example.malltest0.mbg.model.Platform;
import com.example.malltest0.mbg.model.PlatformExample;
import com.example.malltest0.service.PlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PlatformServiceImpl implements PlatformService {

    @Value("platform_key")
    private String platform_key;

    @Autowired
    private PlatformMapper platformMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    @CacheEvict(value = "platform",allEntries = true)
    public int addPlatform(Platform platform) {
        PlatformExample platformExample=new PlatformExample();
        PlatformExample.Criteria criteria=platformExample.createCriteria();
        criteria.andNameEqualTo(platform.getName());
        List<Platform> platforms = platformMapper.selectByExample(platformExample);
        System.out.println(platforms);
        if(platforms.size()!=0){
            return 0;
        }
        platformMapper.insert(platform);
        return 1;
    }

    @Override
    @CacheEvict(value = "platform",allEntries = true)
    public void updatePlatform(Platform platform) {
        platformMapper.updateByPrimaryKey(platform);
    }

    @Override
    @CacheEvict(value = "platform",allEntries = true)
    public void deletePlatform(Long id) {
        platformMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Cacheable(value = "platform")
    public List<Platform> selectAllPlatform() {
        PlatformExample platformExample=new PlatformExample();
        List<Platform> platforms = platformMapper.selectByExample(platformExample);

//        for (Platform platform : platforms) {
//            redisUtils.lPush("platform0",platform);
//        }
        return platforms;
    }

    @Override
    public Platform selectPlatformByName(String name) {
        PlatformExample platformExample=new PlatformExample();
        PlatformExample.Criteria criteria=platformExample.createCriteria();
        criteria.andNameEqualTo(name);
        List<Platform> platforms = platformMapper.selectByExample(platformExample);
        if(platforms!=null){
            return platforms.get(0);
        }
        return null;
    }
}
